<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '活动添加' : '活动修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}
      {{ time }}
      <el-form :model="user">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <el-date-picker
            v-model="time"
            type="datetimerange"
            value-format="timestamp"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeTime"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="changeFirstCate">
            <!-- <el-option label="请选择" :value="0"></el-option> -->
            <el-option
              v-for="item in first_cateArr"
              :key="item.id"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid" @change="changeGoods">
            <el-option
              v-for="item in second_cateArr"
              :key="item.id"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品" label-width="120px">
          <el-select v-model="user.goodsid">
            <!-- <el-option label="请选择" :value="0"></el-option> -->
            <el-option
              v-for="item in goodsname"
              :key="item.id"
              :value="item.id"
              :label="item.goodsname"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { successAlert } from '../../../utils/index'
import { reqgoodslist, reqcatelist, reqseckadd, reqseckinfo ,reqseckedit} from "../../../http/api";
export default {
  props: ["info"],
  data() {
    return {
      time: "",
      user: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      first_cateArr: [],
      second_cateArr: [],
      goodsname:[]
    };
  },
  methods: {
    changeTime() {},
    changeFirstCate() {
      this.user.second_cateid = "";
      this.user.goodsid = ''
      this.getCateSecondList()
    },
    changeGoods(){
        this.getGoodsName()
        // this.users.goodsname = []
        this.user.goodsid = ''
    },
    // 清空
    empty(){
        this.user = {
            title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
        };
        // this.first_cateArr = [];
        // this.second_cateArr = [];
        // this.goodsname = [];
        this.time = []
    },
    update(){
        reqseckedit({
            ...this.user,
            begintime:this.time[0].toString(),
            endtime:this.time[1].toString()
        }).then(res => {
            // console.log(res);
            successAlert(res.data.msg);
            this.info.isshow = false;
            this.empty();
            this.$emit('init')
        })
    },
    // 一级分类
    getCateList() {
      reqcatelist({ istree: true }).then((res) => {
        // console.log(res);
        this.first_cateArr = res.data.list;
      });
    },
    // 二级分类
    getCateSecondList(){
         reqcatelist({ pid: this.user.first_cateid }).then((res) => {
        this.second_cateArr = res.data.list;
      });
    },
    getGoodsName(){
        reqgoodslist().then(res => {
            // console.log('二级分类',res);
            this.goodsname = res.data.list;
              this.goodsname  =  this.goodsname.filter(item => {
             return item.first_cateid == this.user.first_cateid && item.second_cateid == this.user.second_cateid
            });
            console.log(this.goodsname);
        })
    },
    add(){
        reqseckadd({
            ...this.user,
            begintime:this.time[0].toString(),
            endtime:this.time[1].toString()
        }).then(res => {
            console.log('add',res);
            successAlert(res.data.msg);
            this.info.isshow = false;
            this.empty()
            this.$emit('init')
        })
    },
    getOne(id){
        reqseckinfo({id}).then(res => {
            // successAlert(res.data.msg);
            this.user = res.data.list
            this.user.id = id;
            this.getCateSecondList();
            this.getGoodsName();
            this.time = (this.user.begintime + ',' + this.user.endtime).split(',')
            // this.user.second_cateid = 
            console.log(this.user);
        })
    },
    close(){
        if (!this.info.isAdd){
            this.empty()
        }
    }
  },
  mounted() {
    // 一级分类
    this.getCateList()
  },
};
</script>

<style>
</style>